package com.ysy.tz.mfg.controller;

import com.ysy.tz.core.annotation.Service;
import com.ysy.tz.core.engine.ParameterInitializable;
import com.ysy.tz.core.event.BizEvent;
import com.ysy.tz.core.selfcomp.CustomerAlert;
import com.ysy.tz.core.selfcomp.CustomerCheckBox;
import com.ysy.tz.core.selfcomp.CustomerDialogPane;
import com.ysy.tz.core.utils.AESUtil;
import com.ysy.tz.core.utils.FormUtils;
import com.ysy.tz.core.validator.Validator;
import com.ysy.tz.mfg.entity.UserEntity;
import com.ysy.tz.mfg.service.UserEditService;
import java.net.URL;
import java.util.Iterator;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.Node;
import javafx.scene.control.ButtonType;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.layout.FlowPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import org.apache.commons.lang3.StringUtils;

public class UserEditController implements ParameterInitializable {
    @FXML
    private CustomerDialogPane mainPane;
    @FXML
    private TextField userCode;
    @FXML
    private TextField userName;
    @FXML
    private TextField mobilePhone;
    @FXML
    private VBox passwordLabel;
    @FXML
    private PasswordField password;
    @FXML
    private VBox rePasswordLabel;
    @FXML
    private PasswordField rePassword;
    @FXML
    private FlowPane checkBoxGroup;
    private Validator validator = new Validator();
    @Service
    UserEditService userEditService;
    ResourceBundle resourceBundle;
    private boolean isEditUser = false;
    private UserEntity editUser = null;

    public UserEditController() {
    }

    protected void finalize() throws Throwable {
        super.finalize();
        System.out.println(" ----->>>> UserEditController 进行了finalize!!!");
    }

    public void initForm(BizEvent bizEvent) {
        if (bizEvent.getAction().equals("user_Edit")) {
            this.passwordLabel.setVisible(false);
            this.passwordLabel.setManaged(false);
            this.password.setVisible(false);
            this.password.setManaged(false);
            this.rePasswordLabel.setVisible(false);
            this.rePasswordLabel.setManaged(false);
            this.rePassword.setVisible(false);
            this.rePassword.setManaged(false);
            this.isEditUser = true;
            System.out.println("Event------------------->>>>" + bizEvent.getData());

            try {
                FormUtils.setFormValues(this.mainPane, bizEvent.getData());
            } catch (Exception var11) {
                var11.printStackTrace();
            }

            UserEntity userEntity = (UserEntity)bizEvent.getData();
            if (StringUtils.isNotBlank(userEntity.getFuncPermissions())) {
                String[] userFuncPerms = userEntity.getFuncPermissions().split(",");
                Iterator var4 = this.checkBoxGroup.getChildren().iterator();

                while(var4.hasNext()) {
                    Node node = (Node)var4.next();
                    String[] var6 = userFuncPerms;
                    int var7 = userFuncPerms.length;

                    for(int var8 = 0; var8 < var7; ++var8) {
                        String userFuncPerm = var6[var8];
                        CustomerCheckBox ccb = (CustomerCheckBox)node;
                        if (ccb.getKey().equals(userFuncPerm)) {
                            ccb.setSelected(true);
                        }
                    }
                }
            }
        }

    }

    public void initialize(URL location, ResourceBundle resources, Object... params) {
        if (params != null && params.length > 0) {
            BizEvent bizEvent = (BizEvent)params[0];
            this.editUser = (UserEntity)bizEvent.getData();
            this.initForm(bizEvent);
        }

        this.resourceBundle = resources;
        this.validator.createCheck().dependsOn("userCode", this.userCode.textProperty()).withMethod((c) -> {
            String userCode = (String)c.get("userCode");
            if (StringUtils.isBlank(userCode)) {
                c.error("userCode must not be null!");
            }

        }).decorates(this.userCode).immediateNoRecheck();
        this.validator.createCheck().dependsOn("userName", this.userName.textProperty()).withMethod((c) -> {
            String userName = (String)c.get("userName");
            if (StringUtils.isBlank(userName)) {
                c.error("userName must not be null!");
            }

        }).decorates(this.userName).immediateNoRecheck();
        this.validator.createCheck().dependsOn("mobilePhone", this.mobilePhone.textProperty()).withMethod((c) -> {
            String mp = (String)c.get("mobilePhone");
            if (StringUtils.isBlank(mp)) {
                c.error("mobilePhone must not be null!");
            }

        }).decorates(this.mobilePhone).immediateNoRecheck();
        if (!this.isEditUser) {
            this.validator.createCheck().dependsOn("password", this.password.textProperty()).withMethod((c) -> {
                String pw = (String)c.get("password");
                if (StringUtils.isBlank(pw)) {
                    c.error("password must not be null!");
                }

            }).decorates(this.password).immediateNoRecheck();
            this.validator.createCheck().dependsOn("rePassword", this.rePassword.textProperty()).withMethod((c) -> {
                String rpw = (String)c.get("rePassword");
                if (StringUtils.isBlank(rpw)) {
                    c.error("rePassword must not be null!");
                }

            }).decorates(this.rePassword).immediateNoRecheck();
            this.validator.createCheck().dependsOn("password", this.password.textProperty()).dependsOn("rePassword", this.rePassword.textProperty()).withMethod((c) -> {
                String pw = (String)c.get("password");
                String rpw = (String)c.get("rePassword");
                if (!pw.equals(rpw)) {
                    c.error("password not equals rePassword!");
                }

            }).decorates(this.password).decorates(this.rePassword).immediateNoRecheck();
        }

    }

    public void unload() {
    }

    public void submit() {
        if (this.validator.validate()) {
            try {
                UserEntity userEntity = (UserEntity)FormUtils.collectParams(this.mainPane, UserEntity.class);
                String funcPermissions = "";
                Iterator var3 = this.checkBoxGroup.getChildren().iterator();

                while(var3.hasNext()) {
                    Node node = (Node)var3.next();
                    CustomerCheckBox ccb = (CustomerCheckBox)node;
                    if (ccb.isSelected()) {
                        funcPermissions = funcPermissions + ccb.getKey() + ",";
                    }
                }

                if (funcPermissions.length() > 0) {
                    funcPermissions = funcPermissions.substring(0, funcPermissions.length() - 1);
                    userEntity.setFuncPermissions(funcPermissions);
                }

                if (!this.isEditUser) {
                    userEntity.setPassword(AESUtil.Ecodes(userEntity.getPassword()));
                    if (this.userEditService.addUser(userEntity)) {
                        this.hideDialog();
                    } else {
                        (new CustomerAlert(AlertType.WARNING, this.resourceBundle.getString("alert_userAdd_warn"), new ButtonType[0])).showAndWait();
                    }
                } else {
                    userEntity.setUserId(this.editUser.getUserId());
                    userEntity.setPassword((String)null);
                    if (this.userEditService.editUser(userEntity)) {
                        this.hideDialog();
                    } else {
                        (new CustomerAlert(AlertType.WARNING, this.resourceBundle.getString("alert_userEdit_warn"), new ButtonType[0])).showAndWait();
                    }
                }
            } catch (Exception var6) {
                var6.printStackTrace();
                (new CustomerAlert(AlertType.WARNING, this.resourceBundle.getString("alert_userEdit_warn"), new ButtonType[0])).showAndWait();
            }
        }

    }

    public void hideDialog() {
        Stage stage = (Stage)this.mainPane.getScene().getWindow();
        stage.close();
    }
}
